﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace VSAdditionalCommands.Helpers
{
    public static class StringExtensions
    {
        public static string FirstCharToUpper(this string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return input;
            }
            else
            {
                return string.Concat(input[0].ToString().ToUpper(), input.Substring(1));
            }
        }

        public static string FirstCharToLower(this string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return input;
            }
            else
            {
                return string.Concat(input[0].ToString().ToLower(), input.Substring(1));
            }
        }

        /// <summary>
        /// замена строки с сохранением регистра первой буквы заменяемого символа
        /// </summary>
        public static string ReplaceKeepFirstLetterCase(this string input, string findString, string replaceString)
        {
            if (string.IsNullOrEmpty(input) || string.IsNullOrEmpty(findString) || replaceString == null)
            {
                return input;
            }
            else
            {
                if (string.IsNullOrEmpty(replaceString))
                {
                    return Regex.Replace(input, findString, string.Empty, RegexOptions.IgnoreCase);
                }
                else
                {
                    string firstLowerFind = char.ToLower(findString[0]) + findString.Substring(1);
                    string firstUpperFind = char.ToUpper(findString[0]) + findString.Substring(1);
                    string firstLowerReplace = char.ToLower(replaceString[0]) + replaceString.Substring(1);
                    string firstUpperReplace = char.ToUpper(replaceString[0]) + replaceString.Substring(1);
                    string result = input.Replace(firstLowerFind, firstLowerReplace);
                    result = result.Replace(firstUpperFind, firstUpperReplace);
                    return result;
                }
            }
        }
    }
}
